import 'package:apivideo_live_stream/apivideo_live_stream.dart';
import 'package:flutter/material.dart';
import 'package:flutter_frame/app/modules/home/live/types/channel.dart';

import 'types/params.dart';
import 'types/resolution.dart';
import 'types/sample_rate.dart';

class SettingsScreen extends StatefulWidget {
  const SettingsScreen({Key? key, required this.params}) : super(key: key);
  final Params params;

  @override
  _SettingsScreenState createState() => _SettingsScreenState();
}

class _SettingsScreenState extends State<SettingsScreen> {
  int resultAlert = -1;

  @override
  void initState() {
    super.initState();
  }

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Settings'),
        leading: IconButton(
            icon: const Icon(Icons.arrow_back),
            onPressed: () {
              Navigator.pop(
                context,
              );
            }),
      ),
      body: Container(
        width: MediaQuery.of(context).size.width,
        child: Column(
          children: [
            Column(
              children: [
                const Text('Video'),
                ListTile(
                  title:  Text('分辨率${widget.params.getResolutionToString()}}'),
                  // value: Text(widget.params.getResolutionToString()),
                  onTap: () {
                    showDialog(
                        context: context,
                        builder: (context) {
                          return PickerScreen(title: '选择一个分辨率', initialValue: widget.params.video.resolution, values: getResolutionsMap());
                        }).then((value) {
                      if (value != null) {
                        setState(() {
                          widget.params.video.resolution = Resolution.RESOLUTION_480;
                        });
                      }
                    });
                  },
                ),
                ListTile(
                  title:  Text('帧速率${widget.params.video.fps}}'),
                  onTap: () {
                    showDialog(
                        context: context,
                        builder: (context) {
                          return PickerScreen(
                              title: "Pick a frame rate",
                              initialValue: widget.params.video.fps.toString(),
                              values: fpsList.toMap(),
                          );
                        }).then((value) {
                      if (value != null) {
                        setState(() {
                          widget.params.video.fps = 20;
                        });
                      }
                    });
                  },
                ),
                Container(
                    child: Row(
                  children: [
                    Slider(
                      value: (widget.params.video.bitrate / 1024).toDouble(),
                      onChanged: (newValue) {
                        setState(() {
                          widget.params.video.bitrate = (newValue.roundToDouble() * 1024).toInt();
                        });
                      },
                      min: 500,
                      max: 10000,
                    ),
                    Text('${widget.params.video.bitrate}')
                  ],
                )),
              ],
            ),
            Column(
              children: [
                const Text('声音'),
                ListTile(
                  title: Text("通道数${widget.params.getChannelToString()}"),
                  // value: Text(),
                  onTap: () {
                    showDialog(
                        context: context,
                        builder: (context) {
                          return PickerScreen(title: '选择通道的数量', initialValue: widget.params.getChannelToString(), values: getChannelsMap());
                        }).then((value) {
                      if (value != null) {
                        setState(() {
                          widget.params.audio.channel = Channel.stereo;
                        });
                      }
                    });
                  },
                ),
                ListTile(
                  title: Text('比特率${widget.params.getBitrateToString()}'),
                  // value: Text(),
                  onTap: () {
                    showDialog(
                        context: context,
                        builder: (context) {
                          return PickerScreen(
                              title: "Pick a bitrate",
                              initialValue: widget.params.getChannelToString(),
                              values: audioBitrateList.toMap(valueTransformation: bitrateToPrettyString));
                        }).then((value) {
                      if (value != null) {
                        setState(() {
                          widget.params.audio.bitrate = value as int;
                        });
                      }
                    });
                  },
                ),
                ListTile(
                  title: Text('采样率${widget.params.getSampleRateToString()}'),
                  onTap: () {
                    showDialog(
                        context: context,
                        builder: (context) {
                          return PickerScreen(title: "Pick a sample rate", initialValue: widget.params.getSampleRateToString(), values: getSampleRatesMap());
                        }).then((value) {
                      if (value != null) {
                        setState(() {
                          widget.params.audio.sampleRate = value as SampleRate;
                        });
                      }
                    });
                  },
                ),
                // SettingsTile.switchTile(
                //   title: Text('启用回声消除器'),
                //   initialValue: widget.params.audio.enableEchoCanceler,
                //   onToggle: (bool value) {
                //     setState(() {
                //       widget.params.audio.enableEchoCanceler = value;
                //     });
                //   },
                // ),
                // SettingsTile.switchTile(
                //   title: Text('启用噪声抑制器'),
                //   initialValue: widget.params.audio.enableNoiseSuppressor,
                //   onToggle: (bool value) {
                //     setState(() {
                //       widget.params.audio.enableNoiseSuppressor = value;
                //     });
                //   },
                // ),
              ],
                ),
            //     SettingsSection(
            //       title: Text('端点'),
            //       tiles: [
            //         SettingsTile(
            //             title: Text('RTMP端点'),
            //             value: Text(widget.params.rtmpUrl),
            //             onPressed: (BuildContext context) {
            //               showDialog(
            //                   context: context,
            //                   builder: (context) {
            //                     return EditTextScreen(
            //                         title: "输入RTMP端点URL",
            //                         initialValue: widget.params.rtmpUrl,
            //                         onChanged: (value) {
            //                           setState(() {
            //                             widget.params.rtmpUrl = value;
            //                           });
            //                         });
            //                   });
            //             }),
            //     SettingsTile(
            //         title: Text('直播密钥：用于在直播平台上进行直播的一串唯一的字符代码'),
            //         value: Text(widget.params.streamKey),
            //         onPressed: (BuildContext context) {
            //           showDialog(
            //               context: context,
            //               builder: (context) {
            //                 return EditTextScreen(
            //                     title: "输入流键",
            //                     initialValue: widget.params.streamKey,
            //                     onChanged: (value) {
            //                       setState(() {
            //                         widget.params.streamKey = value;
            //                       });
            //                     });
            //               });
            //         }),
            //   ],
            // )
          ],
        ),
      ),
    );
  }
}

class PickerScreen extends StatelessWidget {
  const PickerScreen({
    Key? key,
    required this.title,
    required this.initialValue,
    required this.values,
  }) : super(key: key);

  final String title;
  final dynamic initialValue;
  final Map<dynamic, String> values;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: const Text('Settings')),
        body: Column(
          children: values.keys.map((e) {
            final value = values[e];

            return ListTile(
              title: Text(value!),
              onTap: () {
                Navigator.of(context).pop(e);
              },
            );
          }).toList(),
          // ),
        ));
  }
}

class EditTextScreen extends StatelessWidget {
  const EditTextScreen({Key? key, required this.title, required this.initialValue, required this.onChanged}) : super(key: key);

  final String title;
  final String initialValue;
  final ValueChanged<String>? onChanged;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Settings')),
      body: Column(
        children: [
          Text(title),
          TextField(controller: TextEditingController(text: initialValue), onChanged: onChanged),
        ],
      ),
    );
  }
}
